package com.rslai.base.tool.servertest.objectfactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.rslai.base.tool.servertest.model.KeyValueStore;
import com.rslai.base.tool.servertest.util.CollectionUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class ListFactory extends InstanceFactory {
    protected Object create(Type type, Object value) {
        Collection collection = CollectionUtils.init(type);
        if (value == null) return collection;
        Type elementType = determinedElementType(type);
        if (isJson(value)) {
            return parseFromJson(collection, elementType, value);
        }
        if (isList(value)) {
            return extractFromList(collection, elementType, value);
        }
        return collection;
    }

    private Type determinedElementType(Type genericType) {
        Type elementType = Object.class;
        if ((genericType instanceof ParameterizedType)) {
            elementType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
        }
        return elementType;
    }

    private Object parseFromJson(Collection list, Type elementType, Object value) {
        JSONArray array = (JSONArray) jsonList(StringUtils.trim(value.toString()));
        if (array == null) return null;
        for (Iterator i$ = array.iterator(); i$.hasNext(); ) {
            Object o = i$.next();
            list.add(BeanUtils.create(elementType, o));
        }
        return list;
    }

    private Object extractFromList(Collection list, Type elementType, Object value) {
        if (value instanceof JSONArray) {
            Object o;
            for (Iterator i$ = ((JSONArray) value).iterator(); i$.hasNext(); list.add(BeanUtils.create(elementType, o)))
                o = i$.next();

            return list;
        }
        List values = (List) value;
        KeyValueStore element;
        for (Iterator i$ = values.iterator(); i$.hasNext(); list.add(BeanUtils.create(elementType, element.getValue())))
            element = (KeyValueStore) i$.next();

        return list;
    }

    private boolean isList(Object value) {
        return Collection.class.isAssignableFrom(value.getClass());
    }

    private boolean isJson(Object value) {
        return value instanceof String;
    }

    private Object jsonList(String input) {
        if ((StringUtils.startsWith(input, "[")) && (StringUtils.endsWith(input, "]"))) {
            return JSON.parse(input);
        }
        return null;
    }

    protected boolean support(Type type) {
        if ((type instanceof Class)) {
            return Collection.class.isAssignableFrom((Class) type);
        }
        if ((type instanceof ParameterizedType)) {
            Class rawType = (Class) ((ParameterizedType) type).getRawType();
            return Collection.class.isAssignableFrom(rawType);
        }
        return false;
    }
}
